using System.Linq;
using System.Web.Mvc;
using System.Web;
using System.IO;
using Quickie.Models;
using Quickie.Models.IoC;
using Quickie.Models.Data;
using Quickie.Models.Entities;
using Quickie.Models.ViewModel;
using Quickie.Models.Interfaces;

namespace Quickie.Areas.Admin.Controllers
{
    public class ProductAdminController : Controller
    {
        private IRepository<Category> categories;
        private IRepository<Product> products;

        public ProductAdminController(IRepository<Category> cats, IRepository<Product> prods)
        {
            categories = cats;
            products = prods;
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult List()
        {
            var model = products.GetAll().ToList();
            return View(model);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            var product = products.GetByID(id);
            
            PopulateCategoriesToViewData(product.CategoryID);

            return View(product);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult New()
        {
            var model = new Product();

            PopulateCategoriesToViewData(0);

            return View(model);
        }

        [Authorize(Roles = "Administrator")]
        private void PopulateCategoriesToViewData(int? selectedID)
        {
            var cats = categories.GetAll()
                .OrderBy(x => x.Name)
                .Select(x => new SelectListItem()
                {
                    Text = x.Name,
                    Value = x.ID.ToString(),
                    Selected = x.ID==selectedID
                })
                .ToList();
            cats.Insert(0, new SelectListItem() { Text = "Izaberi kategoriju", Value = "0" });
            ViewData["cats"] = cats;
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Delete(int id)
        {
            Product productToDelete = products.GetByID(id);
            
            products.Delete(productToDelete);
            products.SaveChanges();

            return Redirect(Url.Action("List", "ProductAdmin"));
        }

        [HttpPost,Authorize(Roles = "Administrator")]
        public ActionResult Save(Product product, HttpPostedFileBase image)
        {
            if (!ModelState.IsValid)
            {
               PopulateCategoriesToViewData(product.CategoryID);
                return product.ID == 0 ? View("New", product) : View("Edit", product);
            }

            if(ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    product.ImageData = new byte[image.ContentLength];

                    BinaryReader reader = new BinaryReader(image.InputStream);
                    product.ImageData = reader.ReadBytes(image.ContentLength);
                }
               
            }

            products.Save(product);
            return RedirectToAction("List");
        }

        [Authorize(Roles = "Administrator,Korisnik")]
        public FileContentResult GetImage(int productID)
        {
            Product product = products.GetByID(productID);
            
            return File(product.ImageData.ToArray(), product.ImageMimeType);
        }       
        
    }
}
